<template>
  <el-dialog title="权限分配" :visible="show" @close="hclose">

    <el-tree
      ref="refTree"
      :data="permissionData"
      :props="{ label: 'name' }"
      default-expand-all
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <el-button size="mini" @click="add">提交</el-button>
  </el-dialog>
</template>

<script>
import { assignPerm, getPermissionList, getRoleDetail } from '@/api/user'
import { transListToTreeData } from '@/utils/arr'
export default {
  props: {

    show: {
      type: Boolean,
      required: true
    },
    curid: {
      type: Number,
      required: true
    }
  },
  data: function() {
    return {
      permissionData: []
    }
  },
  async created() {
    const res = await getPermissionList()
    console.log(res)
    this.permissionData = transListToTreeData(res.data, 0)
  },
  methods: {
    hclose() {
      console.log('关闭')
      this.$emit('update:show', false)
    },
    handleCheckChange(e) {
      console.log(e)
    },
    async quanxianguanli() {
      const res = await getRoleDetail(this.curid)
      //   console.log(this.curid)
      const id = res.data.permIds
      console.log(res)
      this.$refs.refTree.setCheckedKeys(id)
    },
    async add() {
      const key = this.$refs.refTree.getCheckedKeys()
      console.log(key)
      await assignPerm({
        id: this.curid,
        permIds: key
      })
      this.hclose()
    }
  }

}
</script>

<style>

</style>
